#include "Visual.h"
#include <fstream>
#include "Debug.h"
#include "Settings.h"
#include "ResourceSystem.h"

std::map<uint16,VisualPack*> VisualPack::s_visualPacks;
std::map<std::string,uint16> VisualPack::s_filenames;

Visual::Visual(void)
{
	m_spriteSheet = NULL;
	m_frames = 0; m_animationsCount = 0;
	m_height = 0; m_width = 0;
}

Visual::~Visual(void)
{
}

void Visual::LoadFromFile(const std::string& filename)
{
	std::ifstream file;
	file.open(filename.c_str());
	if(!file.is_open()) {
		sLog.outDebug("Visual::LoadFromFile() Can't open file: %s", filename.c_str());
		return;
	}

	file >> m_animationsCount >> m_frames;
	file >> m_width >> m_height;

	std::string sprites;
	file >> sprites;
	m_spriteSheet = ResourceSystem::GetImage(sprites, sf::Color::Magenta);
	if(!m_spriteSheet) {
		sLog.outError("Visual::LoadFromFile(): Didn't find spritesheet!");
	}

	file.close();
}

bool VisualPack::LoadFromFile(const std::string& filename)
{
	std::ifstream file;
	file.open(filename.c_str());
	if(!file.is_open()) {
		sLog.outDebug("Visual::LoadFromFile() Can't open file: %s", filename.c_str());
		return false;
	}

	//get the type of the visual pack... unit/building etc? it'll count on how many visuals it should load(or have)
	file >> m_visualType;


	std::string visual_filename;

	while(!file.eof()) {
		file >> visual_filename;
		/*visual_filename = sSettings.GetStringValue(SETTING_GRAPHICS_FOLDER) + "/" + visual_filename;*/
		Visual* temp = new Visual;
		temp->LoadFromFile(visual_filename);
		m_visuals.push_back(temp);
	}
	file.close();

	m_guid = (uint16)s_visualPacks.size();
	s_visualPacks[m_guid] = this;
	s_filenames[filename] = m_guid;
	
	m_count = 1;
	return true;
}

VisualPack::VisualPack(): m_visualType(VISUAL_TYPE_NONE), m_count(0),m_guid(0)
{

}

VisualPack::~VisualPack() 
{
	for (uint16 i = 0; i<m_visuals.size(); i++) {
		delete m_visuals[i];
	}
	m_visuals.clear();
}
//dont use yet.. it's weird!
VisualPack* VisualPack::GetVisualPack(uint16 guid)
{
	VisualPackMap::iterator i = s_visualPacks.find(guid);

	if(i == s_visualPacks.end()) {
		sLog.outDebug("Visual::GetVisualPack(): Couldn't find VisualPack with id: %u", guid);
		return NULL;
	}

	//no reference counting here?..
	
	return i->second;
}

VisualPack* VisualPack::GetVisualPack( const std::string& filename )
{
	FilenameMap::iterator i = s_filenames.find(filename);

	//didn't find it.. load it
	if(i == s_filenames.end()) {
		VisualPack* temp = new VisualPack;
		if (temp->LoadFromFile(filename)) {
			return temp;
		}
		return NULL;
	}
	uint16 guid = i->second;
	VisualPackMap::iterator itr = s_visualPacks.find(guid);
	if(itr == s_visualPacks.end()) {
		sLog.outDebug("Visual::GetVisualPack(): Weird error while loading %s. Filename is in FilenameMap but not on VisualPackMap", filename.c_str());
		return NULL;
	}

	itr->second->SetCount(itr->second->GetCount()+1);
	return itr->second;
}

void VisualPack::FreeVisualPack( const std::string& filename )
{
	FilenameMap::iterator i = s_filenames.find(filename);
	if(i == s_filenames.end()) {
		sLog.outDebug("VisualPack::FreeVisualPack(): Nothing to free(%s is not in the filename map!", filename.c_str());
		return;
	}

	VisualPackMap::iterator itr = s_visualPacks.find(i->second);
	if(itr == s_visualPacks.end()) {
		//already freed ?
		return;
	}
	if(itr->second->GetCount() > 1)
		itr->second->SetCount(itr->second->GetCount()-1);
	else {
		delete itr->second;
		s_visualPacks.erase(itr);
	}

}

void VisualPack::FreeVisualPack( uint16 guid )
{
	VisualPackMap::iterator i = s_visualPacks.find(guid);
	if(i == s_visualPacks.end()) {
		sLog.outDebug("VisualPack::FreeVisualPack(guid): Can't find visual pack with guid = %u", guid);
		return;
	}

	if (i->second->GetCount() > 1) {
		i->second->SetCount(i->second->GetCount()-1);
	} else {
		delete i->second;
		s_visualPacks.erase(i);
	}
}
const std::string& VisualPack::GetName( uint16 guid )
{
	FilenameMap::iterator i = s_filenames.begin(); 
	for(;i!=s_filenames.end(); i++) {
		if (i->second == guid) {
			return i->first;
		}
	}
	return "";		//THIS SHOULDN'T FAIL!! hopefuly..
}